home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / lcppb.zip / LCPP05.ZIP / ACTION.CPP next >
C/C++ Source or Header  |  1991-07-04  |  2KB  |  89 lines

  1. // action.cpp -- Action class module
  2.  
  3. //#include <stream.hpp>
  4. #include <iostream.h>
  5. #include <conio.h>
  6. #include "action.h"
  7.  
  8. /* -- The action's "constructor." Runs when an action object (in
  9. other words, a variable of type action) comes into being. */
  10.  
  11. action::action()
  12. {
  13.   timeAtStart = 3600;           // Seconds to run simulation
  14.   timeRemaining = timeAtStart;  // Time remaining to end
  15. }
  16.  
  17. /* -- Return TRUE if time remaining is greater than 0. The
  18. program's main loop continues the action until this function
  19. returns FALSE. */
  20.  
  21. int action::continues(void)
  22. {
  23.   return timeRemaining > 0;
  24. }
  25.  
  26. /* -- Set time counters to this many seconds. */
  27.  
  28. void action::setTime(int secs)
  29. {
  30.   timeAtStart = secs;
  31.   timeRemaining = timeAtStart;
  32. }
  33.  
  34. /* -- Return time remaining for simulation. */
  35.  
  36. int action::getTime(void)
  37. {
  38.   return timeRemaining;
  39. }
  40.  
  41. /* -- Reduce time remaining for simulation. Minimum resolution,
  42. or "granularity," is one second. Can't reduce time to < 0. */
  43.  
  44. void action::reduceTime(int secs)
  45. {
  46.   if (secs > timeRemaining)
  47.     timeRemaining = 0;
  48.   else
  49.     timeRemaining -= secs;
  50. }
  51.  
  52. /* -- Perform the action. In this case, the function is just a shell.
  53. Later, we'll add programming to simulate a real action. */
  54.  
  55. void action::perform(void)
  56. {
  57.   cout << "\n\nAction! (Press <Space> to continue...";
  58.   while (getch() != ' ') ;   // Pause for <Space> keypress
  59.   reduceTime(900);           // Decrease time remaining 900 secs
  60. }
  61.  
  62. /* -- Display the current action status. Calling display() is the
  63. only way a statement outside of the action object can access the
  64. timeRemaining value. */
  65.  
  66. void action::display(void)
  67. {
  68.   cout << "\n\nTime remaining: " << timeRemaining << " sec.";
  69. }
  70.  
  71. /* -- Display final results. As in action::display(), calling
  72. results() is the only way a statement outside of the action object
  73. can access the private data fields in the action class. */
  74.  
  75. void action::results(void)
  76. {
  77.   cout << "\n\nSimulation results";
  78.   cout << "\n==================";
  79.   cout << "\nTime at start .. : " << timeAtStart << " sec.";
  80.   cout << "\nTime at end .... : " << timeRemaining << " sec.\n";
  81. }
  82.  
  83.  
  84. // Copyright (c) 1990 by Tom Swan. All rights reserved
  85. // Revision 1.00    Date: 09/18/1990   Time: 08:54 am
  86.  
  87. // Revision 1.01    Date: 07/03/1991   Time: 09:07 pm
  88. // Converted for Borland C++ 2.0
  89.